// tool-selector.js
// 工具选择器
class ToolSelector {
    constructor(drawCanvas) {
        // 获取工具按钮引用
        this.drawCanvas = drawCanvas;

        // 注册回调函数
        this.onToolChange = null;
        this.onColorChange = null;
        this.onSizeChange = null;
    }

    // 设置工具按钮活动状态
    setclickingTool(tool) {
        // 移除所有工具的活动状态
        rectTool.classList.remove('clicking');
        drawTool.classList.remove('clicking');
        lineTool.classList.remove('clicking');
        circleTool.classList.remove('clicking');
        textTool.classList.remove('clicking');
        arrowTool.classList.remove('clicking');

        // 添加当前工具的活动状态并设置光标样式
        if (tool === 'rect') {
            rectTool.classList.add('clicking');
            this.drawCanvas.style.cursor = 'crosshair';
        } else if (tool === 'draw') {
            drawTool.classList.add('clicking');
            this.drawCanvas.style.cursor = 'crosshair';
        } else if (tool === 'line') {
            lineTool.classList.add('clicking');
            this.drawCanvas.style.cursor = 'crosshair';
        } else if (tool === 'circle') {
            circleTool.classList.add('clicking');
            this.drawCanvas.style.cursor = 'crosshair';
        } else if (tool === 'text') {
            textTool.classList.add('clicking');
            this.drawCanvas.style.cursor = 'text';
        } else if (tool === 'arrow') {
            arrowTool.classList.add('clicking');
            this.drawCanvas.style.cursor = 'crosshair';
        }

        // 触发工具变化回调
        if (this.onToolChange) {
            this.onToolChange(tool);
        }
    }

    // 设置颜色选择
    setclickingColor(color, event) {
        // 移除所有颜色选项的活动状态
        colorOptions.forEach(option => {
            option.classList.remove('clicking');
        });

        // 添加当前颜色的活动状态
        if (event && event.target) {
            event.target.classList.add('clicking');
        }

        // 触发颜色变化回调
        if (this.onColorChange) {
            this.onColorChange(color);
        }
    }

    // 设置大小选择
    setclickingSize(size, event) {
        // 移除所有大小选项的活动状态
        sizeOptions.forEach(option => {
            option.classList.remove('clicking');
        });

        // 添加当前大小的活动状态
        if (event && event.target) {
            event.target.classList.add('clicking');
        }

        // 触发大小变化回调
        if (this.onSizeChange) {
            this.onSizeChange(size);
        }
    }

    // 初始化事件监听
    initEventListeners() {
        // 工具按钮事件监听
        rectTool.addEventListener('click', () => this.setclickingTool('rect'));
        drawTool.addEventListener('click', () => this.setclickingTool('draw'));
        lineTool.addEventListener('click', () => this.setclickingTool('line'));
        circleTool.addEventListener('click', () => this.setclickingTool('circle'));
        textTool.addEventListener('click', () => this.setclickingTool('text'));
        arrowTool.addEventListener('click', () => this.setclickingTool('arrow'));

        // 颜色选项事件监听
        colorOptions.forEach(option => {
            option.addEventListener('click', (e) => {
                this.setclickingColor(e.target.dataset.color, e);
            });
        });

        // 大小选项事件监听
        sizeOptions.forEach(option => {
            option.addEventListener('click', (e) => {
                this.setclickingSize(e.target.dataset.size, e);
            });
        });
    }

    // 初始化默认状态
    initDefaultState() {
        this.setclickingTool('draw');
        this.setclickingSize('10');

        // 设置默认颜色（如果有默认选项）
        const defaultColorOption = Array.from(colorOptions).find(option => option.dataset.color === '#e74c3c');
        if (defaultColorOption) {
            this.setclickingColor('#e74c3c', { target: defaultColorOption });
        }
    }
}